package cn.k.service.impl;

import cn.k.domain.Appointment;
import cn.k.mapper.AppointmentMapper;
import cn.k.qo.AppointmentQueryObject;
import cn.k.qo.QueryObject;
import cn.k.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    @Transactional
    public void insert(Appointment appointment) {
        //预约单流水号(年月日时分秒+5位随机数)
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        //随机数
        int random=new Random().nextInt(90000)+10000;
        appointment.setAno(date+random);

        //创建时间
        appointment.setCreateTime(new Date());
         appointmentMapper.insert(appointment);
    }

    @Override
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);

    }

    @Override
    public Appointment get(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        return appointment;
    }

    @Override
    public List<Appointment> selectAll() {
        List<Appointment> appointments = appointmentMapper.selectAll();
        return appointments;
    }


    @Override
    public List<Appointment> selectForList(AppointmentQueryObject qo) {
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return appointments;
    }

    @Override
    public PageInfo<Appointment> query(AppointmentQueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"a.create_time desc");
        //查询当前页数据
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return new PageInfo<>(appointments);
    }

    @Override
    public void updateStatus(Long id, int status) {
        appointmentMapper.updateStatus(id,status);
    }
}
